package com.fengniaoxls.fengniaonewretail.webview;

import android.text.TextUtils;

import com.fengniaoxls.fengniaonewretail.app.App;
import com.fengniaoxls.fengniaonewretail.commom.constants.Constants;

import com.fengniaoxls.fengniaonewretail.utils.SPUtils;
import com.freak.httphelper.log.LogUtil;

/**
 * @author Administrator
 * @date 2019/3/28
 */

public class InjectParamsUtil {
    /**
     * 注入参数
     *
     * @param url
     * @return
     */
    public static String injectIsParams(String url) {
        if (url != null && !url.contains("access_token=")) {
            if (url.contains("?")) {
                return url + "&access_token=" + SPUtils.get(App.getInstance().getApplicationContext(), Constants.ACCESS_TOKEN, "");
            } else {
                return url + "?access_token=" + SPUtils.get(App.getInstance().getApplicationContext(), Constants.ACCESS_TOKEN, "");
            }
        } else {
            String newUrl = "";
            String newToken = "";
            String oldToken = url.substring(url.indexOf("access_token=") + 13);
            newToken = (String) SPUtils.get(App.getInstance().getApplicationContext(), Constants.ACCESS_TOKEN, "");
            LogUtil.e("newToken-->" + newToken);
            if (!TextUtils.isEmpty(oldToken)) {
                newUrl = url.replace(oldToken, newToken);
            } else {
                newUrl = url;
            }
            LogUtil.e("newUrl-->" + newUrl);
            return newUrl;
        }
    }

    public static String verrificationToken(String url) {
        if (url != null && !url.contains("access_token=")) {
            if (url.contains("?")) {
                return url + "&access_token=" + SPUtils.get(App.getInstance().getApplicationContext(), Constants.ACCESS_TOKEN, "");
            } else {
                return url + "?access_token=" + SPUtils.get(App.getInstance().getApplicationContext(), Constants.ACCESS_TOKEN, "");
            }
        }
        return url;
    }
}
